package com.itheima.reggie.common;

import jdk.nashorn.internal.runtime.regexp.joni.ast.StringNode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R<String> ExceptionHandler(SQLIntegrityConstraintViolationException ex){
        String message = ex.getMessage();
        if (message.contains("Duplicate entry")){
            String[] split = message.split("'");
            String msg = split[1] + " 已存在";
            return R.error(msg);
        }
        return R.error("失败了");
    }
    @ExceptionHandler(CustomException.class)
    public R<String> ExceptionHandler(CustomException ex){

        return R.error(ex.getMessage());
    }
}
